package com.fyzk.activity.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.fyzk.adapter.DocumentaryMenuAdapter;
import com.fyzk.adapter.FindAdapter;
import com.fyzk.base.BaseFragment;
import com.fyzk.bean.DocumentaryMenuResponse;
import com.fyzk.manager.UIManager;
import com.fyzk.utils.AssetsUtil;
import com.fyzk.view.TopTitleView;
import com.fyzk.view.widget.DividerItemDecoration;
import com.tyjc.lottery.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *
 * @author Administrator
 * @mood
 * @description 发现Fragment
 * @date 2018/9/17 14:44
 * </p>
 */
@ContentView(R.layout.fragment_discover)
public class DiscoverFragment extends BaseFragment {

    @ViewInject(R.id.rv_find)
    RecyclerView mRvFind;
    @ViewInject(R.id.discoverFragUI_topTitleView)
    TopTitleView mToptitle;
    private GridLayoutManager mFindGridLayoutManager;
    private LinearLayoutManager mFindManager;
    private DocumentaryMenuAdapter mDocumentaryMenuAdapter;
    private RecyclerView mRVFindHead;
    private FindAdapter mAdapter;
    private View mHeadView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return x.view().inject(this, inflater, container);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initViews();
    }

    /**
     * 初始化数据
     */
    private void initViews() {
        mToptitle.getRl().setMinimumHeight(150);
        mToptitle.getRl().setPadding(0, 50, 0, 0);
        mFindManager = new LinearLayoutManager(getContext());
        mAdapter = new FindAdapter();
        mRvFind.setLayoutManager(mFindManager);
        mRvFind.setAdapter(mAdapter);
        mHeadView = LayoutInflater.from(getContext()).inflate(R.layout.head_find_view, mRvFind, false);
        mRVFindHead = mHeadView.findViewById(R.id.rv_find_head);
        mFindGridLayoutManager = new GridLayoutManager(getContext(), 3);
        mDocumentaryMenuAdapter = new DocumentaryMenuAdapter();
        mRVFindHead.setLayoutManager(mFindGridLayoutManager);
        mRVFindHead.setAdapter(mDocumentaryMenuAdapter);
//        mAdapter.addHeaderView(mHeadView);
        mAdapter.setHeaderAndEmpty(true);
        List<DocumentaryMenuResponse> mMenuListData = new ArrayList<>();
        List<DocumentaryMenuResponse> mItemListData = new ArrayList<>();
        try {
            String mFindData = AssetsUtil.read(getContext(), "json/findMenuLayout.json");
            String mFindItemData = AssetsUtil.read(getContext(), "json/findItemLayout.json");
            JSONArray mMenuArray = new JSONArray(mFindData);
            JSONArray mItemArray = new JSONArray(mFindItemData);
            for (int i = 0; i < mMenuArray.length(); i++) {
                JSONObject mMenuItem = mMenuArray.getJSONObject(i);
                int mMenuId = mMenuItem.getInt("id");
                String mMenuName = mMenuItem.getString("title");
                String mMenuIcon = mMenuItem.getString("icon");
                String mMenuAction = mMenuItem.getString("action");
                DocumentaryMenuResponse menuResponse = new DocumentaryMenuResponse(mMenuId, mMenuName, mMenuIcon, mMenuAction);
                mMenuListData.add(menuResponse);
            }

            for (int i = 0; i < mItemArray.length(); i++) {
                JSONObject mFindItem = mItemArray.getJSONObject(i);
                int mItemId = mFindItem.getInt("id");
                String mItemName = mFindItem.getString("title");
                String mItemIcon = mFindItem.getString("icon");
                String mItemAction = mFindItem.getString("action");
                String mItemDescribe = mFindItem.getString("describe");
                DocumentaryMenuResponse mItemResponse = new DocumentaryMenuResponse(mItemId, mItemName, mItemIcon, mItemAction, mItemDescribe);
                mItemListData.add(mItemResponse);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mDocumentaryMenuAdapter.setNewData(mMenuListData);
        mAdapter.setNewData(mItemListData);
        mRVFindHead.addOnItemTouchListener(new FindMenuItemClickListener());
        mRvFind.addOnItemTouchListener(new FindItemClickListener());
        DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(getContext(), DividerItemDecoration.VERTICAL);
        dividerItemDecoration.setDrawable(getContext().getDrawable(R.drawable.item_find_divider));
        mRvFind.addItemDecoration(dividerItemDecoration);
    }

    /**
     * 上面的菜单点击事件
     */
    private class FindMenuItemClickListener extends OnItemClickListener {

        @Override
        public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
            List<DocumentaryMenuResponse> mData = adapter.getData();
            UIManager.startActivityByUri(getContext(), mData.get(position).getAction());
        }
    }

    /**
     * item的点击事件
     */
    private class FindItemClickListener extends OnItemClickListener {

        @Override
        public void onSimpleItemClick(BaseQuickAdapter adapter, View view, int position) {
            List<DocumentaryMenuResponse> mData = adapter.getData();
            UIManager.startActivityByUri(getContext(), mData.get(position).getAction());
        }
    }

}
